package com.qfedu.sa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qfedu.common.param.AccountTblAdd;
import com.qfedu.common.vo.PageData;
import com.qfedu.common.vo.R;
import com.qfedu.sa.dao.AccountTblDao;
import com.qfedu.sa.entity.AccountTbl;
import com.qfedu.sa.service.AccountTblService;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

/**
 * @program: Seata01
 * @description: 业务接口实现层，核心内容,类-AccountTblServiceImpl
 * @author: Feri
 * @create: 2024-11-15 08:29:51
 */
@Service
@RequiredArgsConstructor
public class AccountTblServiceImpl implements AccountTblService {

    private final AccountTblDao dao;

    /**
    * 新增内容*/
    @Override
    public R save(AccountTblAdd add) {
        if(add!=null){
            AccountTbl obj=new AccountTbl();
            BeanUtils.copyProperties(add,obj);

            if(dao.insert(obj)>0){
                return R.ok();
            }
        }
        return R.fail("亲，新增失败！");
    }


    /**
    * 查询 所有数据*/
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public Integer queryMoney(Integer uid) {
        LambdaQueryWrapper<AccountTbl> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AccountTbl::getUserId,uid);
        AccountTbl accountTbl=dao.selectOne(wrapper);
        if(accountTbl!=null){
            return accountTbl.getMoney();
        }
        return 0;
    }

    @Override
    public Integer updateMoney(Integer uid, Integer money) {
        //模拟故障
        System.err.println(1/0);
        return dao.updateMoney(uid, money);
    }
}